/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 */
package BusinessLayer;

import Objects.Game;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 *
 * @author zorr
 */
public class Transform {

    public static Document dom;

    public static String createDocument_buyGame(String res) throws JAXBException {

        try {
            /////////////////////////////
            //Creating an empty XML Document

            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("result");
            doc.appendChild(root);

            //create child element, add an attribute, and add to root
//            Element child = doc.createElement("child");
//            root.setAttribute("ergebnis", "true");
//            root.appendChild(child);

            //add a text element to the child
            Text text = doc.createTextNode(res);
            root.appendChild(text);

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
//            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            return xmlString;
        } catch (Exception e) {
            System.out.println(e);
        }

        return null;
    }

    public static String createDocument_getGameList(ArrayList<Game> list) throws JAXBException {

        try {
            /////////////////////////////
            //Creating an empty XML Document

            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("gameList");
            doc.appendChild(root);

            for (int i = 0; i < list.size(); i++) {

                //create child element, add an attribute, and add to root
                Element child = doc.createElement("game");
                root.appendChild(child);

                Element child2 = doc.createElement("name");
                child.appendChild(child2);

                Element child3 = doc.createElement("id");
                child.appendChild(child3);

                Text text = doc.createTextNode(list.get(i).getName());
                child2.appendChild(text);

                Text text2 = doc.createTextNode(String.valueOf(list.get(i).getId()));
                child3.appendChild(text2);
            }

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
//            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            return xmlString;

        } catch (Exception e) {
            System.out.println(e);
        }

        return null;
    }

    public static String createDocument_getMyGames(ArrayList<Game> list) throws JAXBException {

        try {
            /////////////////////////////
            //Creating an empty XML Document

            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("gameList");
            doc.appendChild(root);

            for (int i = 0; i < list.size(); i++) {

                //create child element, add an attribute, and add to root
                Element child = doc.createElement("game");
                root.appendChild(child);

                Element child2 = doc.createElement("name");
                child.appendChild(child2);

                Element child3 = doc.createElement("id");
                child.appendChild(child3);

                Text text = doc.createTextNode(list.get(i).getName());
                child2.appendChild(text);

                Text text2 = doc.createTextNode(String.valueOf(list.get(i).getId()));
                child3.appendChild(text2);
            }

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
//            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            return xmlString;
        } catch (Exception e) {
            System.out.println(e);
        }

        return "aaaaaaa";
    }

    public static String createDocument_getLicense(String res) throws JAXBException {

        try {
            /////////////////////////////
            //Creating an empty XML Document

            
            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("license");
            doc.appendChild(root);

            //create child element, add an attribute, and add to root
//            Element child = doc.createElement("child");
//            root.setAttribute("ergebnis", "true");
//            root.appendChild(child);

            //add a text element to the child
            Text text = doc.createTextNode(res);
            root.appendChild(text);

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
//            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            return xmlString;
        } catch (Exception e) {
            System.out.println(e);
        }

        return null;
    }

    public static String createDocument_getGame(String res) throws JAXBException {

        try {
            /////////////////////////////
            //Creating an empty XML Document
            
            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("encGame");
            doc.appendChild(root);

            //create child element, add an attribute, and add to root
//            Element child = doc.createElement("child");
//            root.setAttribute("ergebnis", "true");
//            root.appendChild(child);

            //add a text element to the child
            Text text = doc.createTextNode(res);
            root.appendChild(text);

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
//            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            return xmlString;
        } catch (Exception e) {
            System.out.println(e);
        }

        return null;
    }
}